package com.tom;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MemberController
 */
public class Order02Controller extends HttpServlet {
        private static final long serialVersionUID = 1L;
        private static final String LIST_ORDER02 = "listOrder02.jsp";
        private static final String ADD_ORDER02 = "order02.jsp";
        private static final String EDIT_ORDER02 = "order02_edit.jsp";

        private Order02DAO dao;

        /**
         * @see HttpServlet#HttpServlet()
         */
        public Order02Controller() {
                super();
                dao = new Order02DAO();
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
         *      response)
         */
        protected void doGet(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {

        	doPost(request, response);
        }
       
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
         *      response)
         */
        protected void doPost(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {
        	request.setCharacterEncoding("UTF-8");
            String forward = LIST_ORDER02;
            String action = request.getParameter("action");
            if (action == null) {
                    forward = LIST_ORDER02;
            } else {
                    if (action.equalsIgnoreCase("listorder02")) {
                            forward = LIST_ORDER02;
                    }
                    if (action.equalsIgnoreCase("addorder02")) {
                            if (request.getParameter("ord02_num") != null) {
                                    // save
                                    String ord02_num = request.getParameter("ord02_num");
                                    String ord02_number = request.getParameter("ord02_number");
                                    String ord02_desc = request.getParameter("ord02_desc");
                                    String ord02_name = request.getParameter("ord02_name");
                                    int ord02_price = Integer.parseInt(request.getParameter("ord02_price"));
                                    int ord02_outday = Integer.parseInt(request.getParameter("ord02_outday"));
                                    String ord02_outtime = request.getParameter("ord02_outtime");
                                    String ord02_address = request.getParameter("ord02_address");
                                    String ord02_memo = request.getParameter("ord02_memo");
                                    Order02 o = new Order02(ord02_num, ord02_number, ord02_desc, ord02_name, ord02_price, ord02_outday, ord02_outtime, ord02_address, ord02_memo);
                                    dao.add(o);
                                    forward = LIST_ORDER02;
                            } else {
                                    forward = ADD_ORDER02;
                            }
                    }
                    if (action.equalsIgnoreCase("edit")) {
                    		String name = request.getParameter("ord02_num");
                            Order02 o = dao.getOrder02ById(name);
                            request.setAttribute("o", o);
                            forward = EDIT_ORDER02;
                    }
            }
            List<Order02> order02s = dao.getAll();
            request.setAttribute("order02s", order02s);
            RequestDispatcher dispatcher = request
                            .getRequestDispatcher(forward);
            dispatcher.forward(request, response);
//        		request.setCharacterEncoding("UTF-8");
//                String action = request.getParameter("action");
//                if (action.equalsIgnoreCase("edit")) {
//                        Order02 o = new Order02(request.getParameter("ord02_num"),
//                        		request.getParameter("ord02_p_number"),
//                        		request.getParameter("ord02_desc"),
//                        		request.getParameter("ord02_name"),
//                        		Integer.parseInt(request.getParameter("ord02_price")),
//                        		Integer.parseInt(request.getParameter("ord02_outday")),
//                        		request.getParameter("ord02_outtime"),
//                        		request.getParameter("ord02_address"),
//                        		request.getParameter("ord02_memo"));
//                        dao.update(o);
//                        List<Order02> order02s = dao.getAll();
//                        request.setAttribute("order02s", order02s);
//                        RequestDispatcher dispatcher = request
//                                        .getRequestDispatcher(LIST_ORDER02);
//                        dispatcher.forward(request, response);
//                }
        }
}

